<template>
  <a-modal
    :title="modalTitle"
    :width="800"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleSubmit"
    @cancel="handleCancel">
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-form-item label="业务ID" :labelCol="labelCol" :wrapperCol="wrapperCol" v-show="false">
          <a-input v-if="modalOpera==1" v-decorator="['id', {rules: [{required: true}]}]" />
        </a-form-item>
        
        <a-form-item label="标题" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['title']" />
        </a-form-item>

        <a-form-item label="分类" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-select
            placeholder="请选择分类"
            v-decorator="[
              'classify',
              {rules: [{ required: true, message: '请选择分类'}],initialValue:'slideshow'}
            ]"
          >
          <!-- {rules: [{ required: true, message: '请选择分类'}],initialValue:'slideshow'} -->
            <a-select-option value="slideshow" title="顶部轮播" >顶部轮播</a-select-option>
            <a-select-option value="bannershow" title="中部轮播" >中部轮播</a-select-option>
          </a-select>
        </a-form-item>

        <a-form-item label="排序号" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['sort']" type="number" oninput="value=value.replace(/[^0-9.]/g,'')" />
        </a-form-item>

        <a-form-item label="是否启用" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-switch
            checked-children="启用"
            un-checked-children="未启用"
            v-decorator="['status',{ rules: [{required: true}],initialValue: false,valuePropName: 'checked'}]"
          />
        </a-form-item>

        <a-form-item label="广告图片" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <upload-file ref="uploadFile" v-decorator="['cover']"></upload-file>
        </a-form-item>
       

        <a-form-item label="跳转路径" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input placeholder="" v-decorator="['pathUrl']" />
        </a-form-item>
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>

  import { insertSlideshow ,selectSlideshowDetails ,updateSlideshow } from '@/api/site/categorySlideshow'
  import uploadFile from './uploadFile.vue'
  import pick from 'lodash.pick'
  export default {
    components: {
      uploadFile
    },
    data () {
      return {
        labelCol: {
          xs: {
            span: 24
          },
          sm: {
            span: 5
          }
        },
        wrapperCol: {
          xs: {
            span: 24
          },
          sm: {
            span: 13
          }
        },
	      treeData: [],
        modalTitle: '',
        modalOpera: 0, // 0：新增 1：编辑
        visible: false,
        confirmLoading: false,
        form: this.$form.createForm(this),
      }
    },
    created () { 
      // this.loadTreeData() 
      // console.log(this.form)
      },
    methods: {
      add () {
        this.modal('新建广告图', 0)
      },
      edit (record) {
        this.modal('编辑广告图', 1)
        selectSlideshowDetails({
          id: record.id
        }).then(response => {
          // console.log(id);
          // console.log(response);
          const result = response.res
          if (result.cover) {
            this.$refs.uploadFile.setValue(JSON.parse(result.cover))
          }
         this.form.setFieldsValue(pick(result, 'id', 'title','classify','sort', 'cover', 'pathUrl'))
         const share = result.status ? true : false
         this.form.setFieldsValue({ 'status': share })
        })
      },
      modal (title, opera) {
        this.modalTitle = title
        this.modalOpera = opera
        this.visible = true
        this.form.resetFields()
      },
      // loadTreeData () {
      //   pageOrg({}).then(response => {
      //     this.treeData = response.res.children
      //   })
      // },
      handleSubmit () {
        const {
          form: {
            validateFields
          }
        } = this
        this.confirmLoading = true
        validateFields((errors, values) => {
          if (!errors) {
            const logo = this.$refs.uploadFile.getValue()
            if (logo) {
              values.covers = JSON.stringify(logo)
            }
            values.status = values.status ? 1 : 0
            values.sort = values.sort ? values.sort : 0
            if (this.modalOpera === 0) {
              insertSlideshow(values).then(response => {
                // console.log(values);
                // console.log(response);
                this.message(response)
              })
            } else if (this.modalOpera === 1) {
              updateSlideshow(values).then(response => {
                // console.log(values);
                // console.log(response);
                this.message(response)
              })
            }
          }
          this.confirmLoading = false
        })
      },
      message (response) {
        if (response.code === 0) {
          this.$message.info(response.msg)
          this.$emit('ok')
          this.handleCancel()
        } else {
          this.$message.error(response.msg)
        }
      },
      handleCancel () {
        this.visible = false
        this.confirmLoading = false
        this.$refs.uploadFile.clear()
      }
    }
  }
</script>
